#include <reg52.h>

#define LED_PORT P2

sbit DO_PIN       = P3^2;
sbit BRIGHT_KEY   = P3^0;
sbit MODE1_KEY    = P3^1;
sbit MODE23_KEY   = P3^3;

unsigned char mode = 1;
unsigned char brightness = 0;

void DelayMS(unsigned int ms) {
    unsigned int i, j;
    for(i = 0; i < ms; i++)
        for(j = 0; j < 114; j++);
}

void UartInit(void) {
    SCON = 0x50;
    TMOD |= 0x20;
    TH1 = 0xFD; TL1 = 0xFD;
    TR1 = 1;
    EA = 1; ES = 0;
}

void SendChar(char c) {
    SBUF = c;
    while(!TI);
    TI = 0;
}

void SendString(char* s) {
    while(*s) SendChar(*s++);
}

void ReportStatus() {
    if(mode == 1) {
        SendString("状态：关，亮度：0%，模式：关灯模式\r\n");
    } else {
        SendString("状态：开，");
        switch(brightness) {
            case 0: SendString("亮度：20%，"); break;
            case 1: SendString("亮度：50%，"); break;
            case 2: SendString("亮度：100%，"); break;
        }
        if(mode == 2)
            SendString("模式：光控模式\r\n");
        else
            SendString("模式：蓝牙模式\r\n");
    }
}

void pwm_led(unsigned char level) {
    unsigned char on_time, i;
    switch(level) {
        case 0: on_time = 2; break;
        case 1: on_time = 5; break;
        case 2: on_time = 10; break;
        default: on_time = 0; break;
    }
    for(i = 0; i < 10; i++) {
        if(i < on_time)
            LED_PORT = 0x00;
        else
            LED_PORT = 0xFF;
        DelayMS(1);
    }
}

void blootooth() {
    if(RI) {
        char cmd = SBUF;
        RI = 0;

        if(cmd == '0') {
            LED_PORT = 0xFF;
            mode = 1;
            ReportStatus();
        } else if(cmd == '1') {
            mode = 3;
            ReportStatus();
        } else if(cmd == 'l') {
            mode = 2;
            ReportStatus();
        } else if(cmd == 'b') {
            mode = 3;
            SendString("保持蓝牙模式\r\n");
        } else if(cmd == 'A') {
            brightness = 0;
            SendString("亮度设置为20%\r\n");
        } else if(cmd == 'B') {
            brightness = 1;
            SendString("亮度设置为50%\r\n");
        } else if(cmd == 'C') {
            brightness = 2;
            SendString("亮度设置为100%\r\n");
        }
    }
}

void main() {
    UartInit();
    LED_PORT = 0xFF;

    while(1) {
        blootooth();  // 每次循环优先检测蓝牙输入

        if(mode == 1) {
            LED_PORT = 0xFF;

            if(MODE1_KEY == 0) {
                while(MODE1_KEY == 0);
                DelayMS(20);
                mode = 2;
                ReportStatus();
                continue;
            }

            // 如果 blootooth() 中改变了 mode，则跳过当前逻辑
            if(mode != 1) continue;
        }

        else if(mode == 2) {
            while(MODE23_KEY != 0) {
                blootooth();
                if(mode != 2) break;

                if(MODE1_KEY == 0) {
                    while(MODE1_KEY == 0);
                    DelayMS(20);
                    mode = 1;
                    ReportStatus();
                    break;
                }

                if(DO_PIN == 1) {
                    pwm_led(brightness);
                } else {
                    LED_PORT = 0xFF;
                }

                if(BRIGHT_KEY == 0) {
                    while(BRIGHT_KEY == 0);
                    DelayMS(20);
                    brightness = (brightness + 1) % 3;
                    ReportStatus();
                }
            }

            if(mode == 2 && MODE23_KEY == 0) {
                while(MODE23_KEY == 0);
                DelayMS(20);
                mode = 3;
                ReportStatus();
            }
        }

        else if(mode == 3) {
            while(MODE23_KEY != 0) {
                blootooth();
                if(mode != 3) break;

                pwm_led(brightness);

                if(MODE1_KEY == 0) {
                    while(MODE1_KEY == 0);
                    DelayMS(20);
                    mode = 1;
                    ReportStatus();
                    break;
                }

                if(BRIGHT_KEY == 0) {
                    while(BRIGHT_KEY == 0);
                    DelayMS(20);
                    brightness = (brightness + 1) % 3;
                    ReportStatus();
                }
            }

            if(mode == 3 && MODE23_KEY == 0) {
                while(MODE23_KEY == 0);
                DelayMS(20);
                mode = 2;
                ReportStatus();
            }
        }
    }
}
